﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using PloobsEngine.SceneControl;
using PloobsEngine.Draw;

namespace Etapa1.SteeringExperiments
{
    public class PursuitOffset : Steering
    {
        public PursuitOffset(IObject leader,Vector2 offset)
            : base(BehaviorType.offset_pursuit)
        {
            
            this.offset = offset;
            this.leader = leader;
                        
            arrive = new Arrive(Vector3.Zero, Deceleration.fast, 25);
        }
        Arrive arrive;
        IObject leader;
        Vector2 offset;

        public override Microsoft.Xna.Framework.Vector3 CalculateSteering(SteeringEntityInfo entity)
        {
            
            Vector3 bx = leader.PhysicObject.FaceVector;            
            Vector2 b1 = VectorUtils.ToVector2(bx);
            Vector2 b2 = VectorUtils.Perpendicular2DNormalized(VectorUtils.ToVector2(leader.PhysicObject.FaceVector));
            Drawing.Draw3dLine(leader.Position, leader.Position + VectorUtils.ToVector3(b2) * 20, Color.Tomato);
           
            Vector2 res = offset.X * b1 + offset.Y * b2;

            float lookAheadTIme = (leader.Position - entity.Obj.Position).Length() / (leader.PhysicObject.Velocity.Length() + entity.Obj.PhysicObject.Velocity.Length());
            arrive.Target = VectorUtils.ToVector3(res) + leader.Position + leader.PhysicObject.Velocity * lookAheadTIme * 0.5f;
            
            Drawing.DrawCircleOrDisk(2, Vector3.Up, arrive.Target, Color.Yellow, 100,false, true);
            return arrive.CalculateSteering(entity);
            
        }
    }
}
